package com.jiusit.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.filechooser.FileSystemView;


public class FileUtil {
	public static void download(String urlString, String filename) throws Exception {
	    // 构造URL
	    URL url = new URL(urlString);
	    // 打开连接
	    URLConnection con = url.openConnection();
	    // 输入流
	    InputStream is = con.getInputStream();
	    // 1K的数据缓冲
	    byte[] bs = new byte[1024];
	    // 读取到的数据长度
	    int len;
	    // 输出的文件流
	    OutputStream os = new FileOutputStream(filename);
	    // 开始读取
	    while ((len = is.read(bs)) != -1) {
	      os.write(bs, 0, len);
	    }
	    // 完毕，关闭所有链接
	    os.close();
	    is.close();
	} 
	public static StringBuffer imagToBinary(String filename) throws Exception {
		File file = new File(filename);  
        FileInputStream fis; 
        StringBuffer str = new StringBuffer();// 不建议用String  
        try  
        {  
            fis = new FileInputStream(file);  
            byte[] b;  
            b = new byte[fis.available()];  
           
            fis.read(b);  
            for (byte bs : b)  
            {  
                str.append(Integer.toBinaryString(bs));// 转换为二进制  
            }  
            System.out.println(str);  
            fis.close();  
          
        }  
        catch (FileNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        return str;
	} 
}